﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jumpton.SFA.Server.BusinessRule.InStoreDataMeasures;
using Jumpton.SFA.Server.Entity;
using Jumpton.Persistent;

/// <summary>
/// 常规指标维护
/// Update By:Allen Jiang
/// Update Time:2008-12-26
/// Description:
///     修改常规指标维护在一个页面。
/// </summary>
public partial class InStoreDataMeasures_MeasuresDetail_DragSelect_List : PageBase
{
    
    private const string REQUST_ITEMID = "IID";
    private int IID = 0;
    public string allids = "";
    public string sourcedivs = "";
    public string targetdivs = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString[REQUST_ITEMID] != null)
        {
            int.TryParse(Request.QueryString[REQUST_ITEMID], out IID);
        }
        else
        {
            ShowMessage("非法访问或参数错误", "MeasuresGroup_List.aspx");
            return;
        }
        if (!IsPostBack)
        {
            GetMeasuresDetailBind();
            ViewState["url"] = Request.UrlReferrer==null?"MeasuresGroup_List.aspx":Request.UrlReferrer.ToString();
        }
    }

    /// <summary>
    /// 数据绑定
    /// </summary>
    public void GetMeasuresDetailBind()
    {
        string where = "";
         
         if (txt_key.Text != "")
         {
             where += " AND (CODE LIKE '%" + txt_key.Text + "%' OR NAME  LIKE '%" + txt_key.Text + "%')";
         }
         if (ddl_active.SelectedValue != "")
         {
             where += " AND ACTIVE="+ddl_active.SelectedValue;
         }
        MeasuresDetailManager mdm = new MeasuresDetailManager();
        ObjectTable ot= mdm.GetOtherMeasuresDetailTableByItemID(MyStateManager.DomainID,IID,where);
        int i=1;
        foreach (MEASURE_DETAIL md in ot)
        {
            allids += "#d" + i + ",";
            sourcedivs += "<div id=\"d" + i + "\" class=\"drag\" measuredata=\"" + md.ID + "\">" + md.NAME + "</div>";
            i++;
        }
        ot = mdm.GetMeasuresDetailTableByItemID(MyStateManager.DomainID, IID);
        HiddenField1.Value = "";
        foreach (MEASURE_DETAIL md in ot)
        {
            allids += "#d" + i + ",";
            targetdivs += "<div id=\"d" + i + "\" class=\"drag\" measuredata=\"" + md.ID + "\">" + md.NAME + "</div>";
            HiddenField1.Value += "," + md.ID;
            i++;
        }
        allids = allids.TrimEnd(',');
       
    }


    protected void btnSearch_Click(object sender, EventArgs e)
    {
        GetMeasuresDetailBind();
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        MeasuresDetailManager mdm = new MeasuresDetailManager();
        string value = HiddenField1.Value.TrimStart(',');
        string[] IDS = value.Split(',');
        MEASURE_ITEM_DETAIL mid=null;
        decimal time=Jumpton.SFA.Server.BusinessRule.BusinessLogic.GetDateTimeDecimal(DateTime.Now);
        string msg = "";
        if (mdm.DelteItemDetailByItemID(MyStateManager.DomainID, IID))
        {
            if (value != "")
            {
                int sequence = 1;
                foreach (string iid in IDS)
                {
                    mid = new MEASURE_ITEM_DETAIL();
                    mid.MEASUERITEMSID = IID;
                    mid.MEASUERDETAILID = decimal.Parse(iid);
                    mid.CREATETIME = time;
                    mid.LAST_UPDATE_TIME = time;
                    mid.DOMAINID = MyStateManager.DomainID;
                    mid.LAST_UPDATER = MyStateManager.UserID.ToString();
                    mid.SEQUENCE = sequence++;
                    mid.ACTIVE = true;
                    if (!mdm.Save(mid))
                    {
                        msg += mid.MEASUERDETAILID + ",";
                    }
                }
            }
        }
        else
        {
            ShowMessage("保存失败",Request.Url.ToString());
            return;
        }

        if (msg != "")
        {
            ShowMessage("保存失败，" + msg + "没有保存成功！");
        }
        else
        {
            ShowMessage("保存成功", ViewState["url"].ToString());
        }
    }
}
